"""
@name : 三创购物系统.py
@author : chenlu
@projectname: 字典练习.py
@time : 2022/2/18
"""
# 用户信息
user_information = {'root':'123', 'admin':'234', 'chen':'456'}
# 商店里所有商品
thing_list = ['apple', 'orange', 'pear']
# 商品对应的价格
money = [1.2, 2.3, 2.1]
# 用户账户余额
# total = 34
# 判断用户是否登录
username_list = []
# 判断用户是否输入预算
budget_list = []
# 购物车
shop_list = []
# 用户买的商品
usershop = []
# 购买的商品对应的数量
num = []

def login():
    '''登录'''
    print("登录系统".center(40,'*'))
    count = 0
    while True:
        if username_list:
            print("你已登录")
            break
        username = input("请输入用户名：")
        if username != 'exit':
            passwd = input("请输入密码：")
            if passwd != 'exit':
                if username in user_information:
                    if passwd == user_information[username]:
                        print("登录成功")
                        username_list.append(username)
                        break
                    else:
                        print("登录失败")
                else:
                    print("登录失败")
            else:
                break
        else:
            break



def budget_information():
    '''购物预算'''
    print("购物预算".center(40, '*'))
    print("如果需要两个apple：apple*2")
    while True:
        budget = input("请输入您的购物预算(按q进入购物模式)：")
        if budget != 'exit':
            if budget != 'q':
                locate = budget.find('*')
                if budget[:locate] in thing_list:
                    budget_list.append(budget)
                else:
                    print("非常抱歉，我们商店暂时没有此商品")
            else:
                print(budget_list)
                break
        else:
            return


def thing_information():
    '''商品信息'''
    print("商品信息".center(40,'*'))
    thing = {'F001':{'name':'apple','price':1.2}, 'F002':{'name':'orange','price':2.3}, 'F003':{'name':'pear','price':2.1}}
    for i in thing:
        print(i)
        print(thing[i])

def shop():
    '''购物'''
    print("购物".center(40,'*'))
    global total
    total = 34
    while True:
        print("按0：商品信息；按1：购物车；按2：结算，退出系统")
        shop1 = input("请输入您需要购买的东西：")
        if str(shop1) == 'exit':
            shop_list.clear()
            break
        elif shop1 in thing_list:
            if total > money[thing_list.index(shop1)]:
                shop_list.append(shop1)
                usershop.append(shop1[:shop1.find(',')])
                num.append(shop1[shop1.find(',') + 1:])
                total -= money[thing_list.index(shop1)]
            else:
                print("账户余额不足")
                a = input("如果您需要充钱，请输入金额：")
                if a != 'exit':
                    total += int(a)
                else:
                    break
        elif int(shop1) in range(5):
            if shop1 == '0':
                thing_information()
            elif shop1 == '1':
                shopping_information()
            elif shop1 == '2':
                logout()
                break
            elif shop1 == 'exit':
                break
        else:
            print("没有该商品")

def shopping_information():
    '''购物车'''
    print("购物车".center(40,'*'))
    print(shop_list)
    return

def logout():
    '''退出系统'''
    print("退出系统".center(40,'*'))
    print("这是您购买的东西")
    print(shop_list)
    print("这是您所剩的余额")
    print(total)

def fun():
    login()
    while not username_list:
        print("请先登录")
        a = input("您是否要退出登录(Y/N)：")
        if a == 'Y':
            return
        elif a =='N':
            login()
        else:
            print("请输入Y/N")

    budget_information()
    if not budget_list:
        print("如果您要购物需输入购物预算")
        b = input("您是否要退出购物系统(Y/N):")
        if b =='Y':
            budget_list.clear()
            return
        elif b == 'N':
            budget_information()
        else:print("请输入Y/N")
    else:
        c = input("是否进入购物模式(Y/N):")
        if c == 'Y':
            shop()
        elif c == 'N':
            return
        else:print("请输入Y/N")

fun()


